<template>
  <div class="album">
    <header>
      <el-button class="btn">
        <div style="display: flex;align-items: center;"><span class="icon-setting"></span><span
            class="btn-text">设置</span></div>
      </el-button>
      <div class="title">
        <span class="icon-xiangche"></span>
        <span class="text">相册管理</span>
      </div>
      <p class="desc">通过分析您的日常文件、笔记、照片，持续构建和优化您的知识体系，让信息整理和获取变得轻松而直观。</p>
    </header>
    <el-input v-model="input" style="width: 100%;height: 50px;margin: 25px 0;" placeholder="搜索图片">
      <template #prefix>
        <span class="icon-sousuo"></span>
      </template>
      <template #suffix>
        <el-button class="btn" type="primary" @click="querySubmit">AI搜索</el-button>
      </template>
    </el-input>
    <el-scrollbar style="width: 100%;height: 100%;padding-bottom: 10px;">
      <el-card class="main" v-if="isCardAlert === 'main'">
        <div class="renwu" v-if="peopleImgList.length > 0">
          <div class="renwu-header">
            <span class="left">人物</span>
            <span class="right" @click="isCardAlert = 'renwu'">全部</span>
            <span class="icon-right" @click="isCardAlert = 'renwu'"></span>
          </div>
          <el-scrollbar style="width: 100%;">
            <div class="imgList">
              <div class="imgItem" v-for="item in peopleImgList"> <el-avatar @click="opRenwu2(item)" :size="64"
                  :src="item.url" /><span class="name">{{ item.name }}</span>
              </div>
            </div>

          </el-scrollbar>
        </div>
        <div class="renwu" v-if="addrImgList !== {}">
          <div class="renwu-header">
            <span class="left">地点</span>
            <span class="right" @click="isCardAlert = 'didian'">全部</span>
            <span class="icon-right" @click="isCardAlert = 'didian'"></span>
          </div>
          <el-scrollbar style="width: 100%;">
            <div class=" imgList">
              <div class="imgItem" v-for="(item,key) in addrImgList" @click="opendidianAlert(key)"> <el-avatar shape="square" :size="80"
                  :src="getImgUrl(item)" />
                <span class="name dd" style="color: #FFFFFF">{{key}}</span>
              </div>
            </div>

          </el-scrollbar>
        </div>
        <div class="checkbox">

          <!--          <el-checkbox v-model="checked1" label="文件名" size="large" />-->
        </div>
        <el-radio-group style="width:  100%;" v-model="tabPosition" size="small" v-if="tagList.length > 0" @change="tabChange">
          <el-radio-button value="全部">全部</el-radio-button>
          <el-radio-button v-for="item in tagList" :key=item :value=item>{{ item }}</el-radio-button>
          <el-button size="small" @click="labelAllAlert = true" style="margin-left: auto;">三</el-button>
        </el-radio-group>
        <div class="renwu" v-for="(item, key) in dateImg">
          <div class="renwu-header">
            {{ key }}
          </div>
          <el-scrollbar style="width: 100%;">
            <div class=" imgList">
              <div class="imgItem" v-for="ite in item">
                <el-avatar shape="square" :size="80" :src=getImgUrl(ite) />
<!--                <span class="name dd">添加名字</span>-->
                <el-checkbox  :value = "ite" :key = "ite" @change = "changeImg" v-if="peopleImgList.length == 0 && addrImgList.length == 0"></el-checkbox>
              </div>
            </div>
          </el-scrollbar>
          <div height="50px" class="search-button" v-if = "checkImg.length > 0">
            <el-button @click="cancelCheck" plain type="primary">取消</el-button>
            <el-button @click="downloadImg" plain type="primary">下载({{checkImg.length}})</el-button>
          </div>
        </div>
      </el-card>
      <!-- 点击人物进入的页面 -->
      <el-card class="main" v-if="isCardAlert === 'renwu'">
        <div class="renwu" v-if="peopleImgList.length > 0">
          <div class="renwu-header ">
            <span class="icon-left" @click="backMain"></span>
            <span class="left" @click="isCardAlert = 'main'">人物</span>
            <span class="right"></span>
          </div>
          <div class="imgList flexs">
            <div class="imgItem" v-for="item in peopleImgList"> <el-avatar @click="opRenwu2(item)" :size="64"
                :src="item.url" />
              <span @click="EditName(item)" class="name">{{ item.name }}</span>
            </div>
          </div>
        </div>
      </el-card>
      <!-- 点击人物进入的页面点击头像 进入的页面 -->
      <el-card class="main" v-if="isCardAlert === 'renwu2'">
        <div class="renwu" v-if="peopleImgList.length > 0">
          <div class="renwu-header">
            <span class="icon-left" @click="backMain"></span>
            <span class="right"></span>
          </div>
          <div class="imgList" style="width: 100%; display: flex; justify-content: center;">
            <div class="imgItem">
              <el-avatar :size="64" :src="rowData.url" />
              <span @click="EditName(rowData)" class="name"
                style="display: flex;justify-content: center;margin: 6px 0;">{{
                  rowData.name
                }}<span class="icon-edit"></span> </span>
              <el-button round size="small" @click="openAddLabelAlert">添加标签</el-button>
            </div>
          </div>
        </div>
        <el-radio-group v-model="tabPosition" size="small" v-if="tagList.length > 0" @change="tabChange">
          <el-radio-button value="全部">全部</el-radio-button>
          <el-radio-button v-for="item in tagList" :key=item :value=item>{{ item }}</el-radio-button>
        </el-radio-group>
        <div class="renwu" v-for="(item, key) in dateImg">
          <div class="renwu-header">
            {{ key }}
          </div>
          <el-scrollbar style="width: 100%;">
            <div class=" imgList">
              <div class="imgItem" v-for="ite in item">
                <el-avatar shape="square" :size="80" :src=getImgUrl(ite) />
<!--                <span class="name dd">添加名字</span>-->
              </div>
            </div>
          </el-scrollbar>
        </div>
      </el-card>
      <!-- 点击地点进入的页面 -->
      <el-card class="main" v-if="isCardAlert === 'didian'">
        <div class="renwu" v-if="addrImgList !== 0">
          <div class="renwu-header">
            <span class="icon-left" @click="isCardAlert = 'main'"></span>
            <span class="left" @click="isCardAlert = 'main'">地点</span>
            <span class="right"></span>
          </div>
          <div class=" imgList flexs">
            <div class="imgItem" v-for="(item,key) in addrImgList" @click="opendidianAlert(key)"> <el-avatar shape="square"
                :size="80" :src="getImgUrl(item)" /><span class="name dd" style="color: #FFFFFF">{{ key }}</span>
            </div>
          </div>
        </div>
      </el-card>
      <!-- 点击市打开 市区地点图片 -->
      <el-card class="main" v-if="isCardAlert === 'didian2'">
        <div class="renwu" v-if="addrImgList !== {}">
          <div class="renwu-header">
            <span class="icon-left" @click="backMain"></span>
            <span class="left" @click="isCardAlert = 'main'">{{ rowData }}</span>
            <span class="right"></span>
          </div>
          <!-- 日期加图片 -->
          <div class="renwu" v-for="(item, key) in dateImg" style="margin-top: 30px">
            <div class="renwu-header">
              {{ key }}
            </div>
            <el-scrollbar style="width: 100%;">
              <div class=" imgList">
                <div class="imgItem" v-for="ite in item">
                  <el-avatar shape="square" :size="80" :src=getImgUrl(ite) />
<!--                  <span class="name dd">添加名字</span>-->
                </div>
              </div>
            </el-scrollbar>
          </div>
        </div>
      </el-card>
    </el-scrollbar>
    <!-- 添加名字 -->
    <el-dialog class="editNameMian" style="padding: 0;" align-center v-model="editNameAlert" :show-close="false"
      width="350">
      <div class="editName">
        <div class="title">修改名字</div>
        <el-form :model="rowData" class="demo-form-inline" ref="NameFormRef" style="width: 60%;">
          <el-form-item prop="name" style="width: 100%;"
            :rules="[{ required: true, message: '请输入名字', trigger: 'blur' }, { max: 10, message: '名字不得超过10个字符', trigger: 'change' }]">
            <el-input v-model="rowData.name" style="width: 100%;margin: 10px 0;" placeholder="Please input" />
          </el-form-item>
        </el-form>
        <div class="btns">
          <el-button class="btn" @click="editNameAlert = false">取消</el-button>
          <el-button class="btn" @click="editNameSubmit">确定</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 添加标签-->
    <el-dialog class="editNameMian" style="padding: 0;" align-center v-model="addLabelAlert" width="350">
      <div class="editName">
        <div class="title" style="margin: 0;">添加标签</div>
<!--        <el-checkbox-group v-model="label" style="padding: 20px;margin-bottom: 20px;" @change = "TagTabChange">-->
<!--          <el-checkbox-button v-for="item in tagList" :key="item" :label="item" :value="item" @change = "TagChange"/>-->
<!--        </el-checkbox-group>-->
        <el-checkbox-group v-model="label" size="large" style="padding: 20px;margin-bottom: 20px;" @change = "TagTabChange">
          <el-checkbox-button v-for="city in ['我自己', '好友', '孩子', '家人', '伴侣', '同事', '其他']" :key="city" :value="city" @change = "TagChange">
            {{ city }}
          </el-checkbox-button>
        </el-checkbox-group>
      </div>
    </el-dialog>
    <!-- 更多标签 -->
    <el-dialog class="moreTags" style="padding: 0;height:550px;" align-center v-model="labelAllAlert" width="600">
        <div class="editName">
          <div class="title" style="margin: 0;">更多标签</div>
          <div class="labelAll">
            <div class="labelAllItem">
            <el-checkbox-button style="margin:4px 5px;" key="全部" value="全部">
              全部
            </el-checkbox-button>
            </div>
            <div class="labelAllItem" v-for="(item,key) in classTagList">
              <h4 style="margin: 5px 0;">{{ key }}</h4>
              <el-checkbox-group v-model="labelAll">
                <el-checkbox-button style="margin:4px 5px;" v-for="ite in item"
                  :key="ite" :value="ite">
                  {{ ite }}
                </el-checkbox-button>
              </el-checkbox-group>
            </div>
          </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import {
  getAllCityInfo,
  addIdAttribute,
  removeIdAttribute,
  getAllPeople,
  getAllPhotoTag,
  getAllImagesByTimeSort,
  getImgUrl,
  searchPhoto,
  getCityByTimeSort,
  getTagImagesByTimeSort,
  reNameId,
  getAllTags, getPeopelByTimeSort
} from "@/api/index";
const tabPosition = ref('全部')
const peopleImgList = ref([])
const dateImg = ref()
const tagList = ref([])
// const addrImgList = ref([{ name: '骚等1', url: '' }, { name: '骚等2', url: '' }, { name: '骚等3', url: '' }, { name: '骚等4', url: '' }, { name: '骚等5', url: '' }, { name: '骚等6', url: '' }, { name: '骚等7', url: '' }, { name: '骚等8', url: '' }, { name: '骚等9', url: '' }, { name: '骚等', url: '' }, { name: '骚等', url: '' }, { name: '骚等', url: '' }])
const addrImgList = ref([])
const input = ref('')
const isCardAlert = ref('main')
const editNameAlert = ref(false)
const checkImg = ref([])
const addLabelAlert = ref(false)
const label = ref([])
const NameFormRef = ref(null)
const rowData = ref({ name: '', url: '',id:'' })
const labelAll = ref([])
const labelAllAlert = ref(false)
const classTagList = ref({})

onMounted(async () => {
  label.value = [];
  //获取人物数据图片
  const { data, code } = await getAllPeople();
  if (code == 200) {
    data.forEach(item => {
      peopleImgList.value.push({ id: item.id, name: item.name, url: getImgUrl(item.file_path[0]), attribute: item.attribute })
    })
    console.log(data)
  }
  //获取标签数据
  const response = await getAllPhotoTag();
  tagList.value = response.data
  console.log(tagList.value)
  const response2 = await getAllImagesByTimeSort();
  dateImg.value = response2.data
  const response3 = await getAllCityInfo();
  addrImgList.value = response3.data
  const response4 = await getAllTags()
  classTagList.value = response4.data

})

const cancelCheck = async () =>{

}

const downloadImg = async () => {
  checkImg.value.forEach(item => {
    downloadURL(item)
  })
}
const downloadURL = (url) =>{

  const link = document.createElement('a')
  link.href = getImgUrl(url)
  link.download = '图片' + new Date().getTime()
  document.body.appendChild(link)
  link.click()

  // 清理
  document.body.removeChild(link)
  URL.revokeObjectURL(url)
}

const tabChange = async(key) =>{
    if(key === '全部'){
      const response2 = await getAllImagesByTimeSort();
      dateImg.value = response2.data
    }else {
      const response = await getTagImagesByTimeSort(key);
      if (response.code === 200) {
        dateImg.value = response.data
      }
    }
}

const backMain = () =>{
  // lable.value = []
  isCardAlert.value = 'main'
  querySubmit()
}

const TagTabChange = (key) =>{
  // console.log(key)
}
const TagChange = (e,name) =>{
  if(e){
    addIdAttribute({id_name:rowData.value.id,attribute:name.target._value})
  }else{
    removeIdAttribute({id_name:rowData.value.id,attribute:name.target._value})
  }
}

const changeImg = (e,name) =>{
  console.log(name.target._value)
  if(e){
    checkImg.value.push(name.target._value)
    console.log(checkImg.value)
  }else{
    checkImg.value = checkImg.value.filter(item => item != name.target._value)
    console.log(checkImg.value)
  }
}

const querySubmit = async () => {
  if (input.value != '') {
    const { data } = await searchPhoto({ query: input.value, search_num: 10 });
    dateImg.value = data
    peopleImgList.value = []
    tagList.value = []
  } else {
    peopleImgList.value = []
    tagList.value = []
    dateImg.value = []
    const { data, code } = await getAllPeople();
    if (code == 200) {
      data.forEach(item => {
        peopleImgList.value.push({
          id: item.id,
          name: item.name,
          url: getImgUrl(item.file_path[0]),
          attribute: item.attribute
        })
      })
    }
    //获取标签数据
    const response = await getAllPhotoTag();
    tagList.value = response.data
    const response2 = await getAllImagesByTimeSort();
    dateImg.value = response2.data
    console.log(dateImg.value)
  }
}
//添加名字
const EditName = (row) => {
  editNameAlert.value = true
  console.log(row)
  rowData.value = row
}
//修改名字提交
const editNameSubmit = async () => {
  NameFormRef.value.validate(async (valid) => {
    if (!valid) return
    reNameId({old_id_name:rowData.value.id,name:rowData.value.name})
    editNameAlert.value = false
  })
}
//打开人物2页面
const opRenwu2 = async(row) => {
  isCardAlert.value = 'renwu2'
  if(row.attribute !== undefined){
    label.value = row.attribute
  }
  tagList.value = []
  rowData.value = row
  console.log(row)
  const {data} = await getPeopelByTimeSort(row.id);
  dateImg.value = data
}
const openAddLabelAlert = () => {
  addLabelAlert.value = true
}
//打开地点2个页面
const opendidianAlert = async (row) => {
  console.log(row)
  rowData.value = row
  isCardAlert.value = 'didian2'
  const response = await getCityByTimeSort(row)
  dateImg.value = response.data
}
</script>

<style lang="scss" scoped>
.album {
  width: 100%;
  height: 100%;
  align-items: center !important;
  padding: 0 100px;
  padding-top: 84px;
  flex-direction: column;
  position: relative;
  overflow: hidden;

  header {

    .btn {
      position: absolute;
      right: 30px;
      top: 30px;

      .icon-setting {
        width: 16px;
        height: 16px;
        background: url('@/icons/setting.svg') no-repeat center;
        background-size: 16px;
        margin-right: 2px;
      }

      .btn-text {
        color: #191919;
        text-align: center;
        font-size: 12px;
      }
    }

    .title {
      display: flex;
      align-items: center;
      justify-content: center;

      .icon-xiangche {
        display: flex;
        width: 24px;
        height: 24px;
        background: url('@/icons/相册管理.svg') no-repeat center;
        background-size: 24px;
        margin-right: 2px;
      }

      .text {
        color: #191919;
        font-size: 17px;
        font-weight: 600;
      }

    }

    .desc {
      color: #333;
      font-size: 14px;
      margin-top: 11px;
    }
  }

  .icon-sousuo {
    display: flex;
    width: 16px;
    height: 16px;
    background: url('@/assets/image/search1.svg') no-repeat center;
    background-size: 16px;
    margin-right: 2px;
  }

  .main {
    width: 100%;
    margin-top: 32px;
    overflow-y: auto;

    .flexs {
      display: flex;
      flex-wrap: wrap;

      .imgItem {
        margin-bottom: 20px !important;
      }

      width: 100%;
    }

    .renwu {
      margin-bottom: 24px;

      .search-button {
        position: absolute;
        bottom: 0;
        left: 0;
        display: flex;
        justify-content: end;
        align-items: center;
        width: 100%;
        height: 50px;
        border-top: 1px solid rgba(0, 0, 0, 0.10);
        background: #FFF;

        button {
          width: 50%;
          height: 100%;
          border-radius: 0;
          border: none;

          &:nth-child(1) {
            border-right: 1px solid rgba(175, 175, 175, 0.466);
          }
        }
      }

      .renwu-header {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .icon-right {
          display: flex;
          color: #666;
          width: 16px;
          height: 16px;
          background: url('@/icons/right.svg') no-repeat center;
          background-size: 16px;
          margin-right: 2px;
        }

        .icon-left {
          display: flex;
          color: #666;
          width: 16px;
          height: 16px;
          background: url('@/icons/关闭.svg') no-repeat center;
          background-size: 16px;
          margin-right: 2px;
        }

        .left {
          font-size: 14px;
        }

        .right {
          color: #666;
          font-size: 12px;
          margin-left: auto;
        }

      }

      .imgList {
        display: flex;
        width: 100%;
        margin: 20px 0;

        .imgItem {
          margin: 0 16px;
          text-align: center;
          position: relative;
          display: flex;
          flex-direction: column;
          .el-checkbox {
            position: absolute;
            left: 60px; /* 根据el-avatar的size调整 */
            opacity: 0.7;
          }
          .name {
            color: #191919;
            font-size: 12px;
            //width: 50px;
            //white-space: nowrap;
            //overflow: hidden;
            //text-overflow: ellipsis;
          }

          .dd {
            position: absolute;
            width: 100%;
            bottom: 15px;
            left: 50%;
            transform: translate(-50%);
          }
        }
      }
    }
  }

  .checkbox {
    :deep(.el-checkbox__inner) {
      border-radius: 50%;
    }

    padding-left: 8px;
  }

  :deep(.el-radio-button__inner) {
    border-radius: 57px;
    margin: 5px;
  }

  .moreTags{
    .editName {
      display: flex;
      flex-direction: column;
      overflow-y: auto;

      .title {
        text-align: center;
        color: #000D3F;
        font-size: 15px;
        font-weight: 600;
        margin: 10px 0;
      }

      .labelAll {

        margin: 15px 0;

        :deep(.el-checkbox-button__inner) {
          border-radius: 25px;
          border: 1px solid #ccc;
        }

        .labelAllItem {
          margin-left: 20px;
          //margin: 40px 0;
        }
      }

      .btns {
        width: 100%;

        .btn {
          width: 50%;
          height: 60px;
          margin: 0;
          margin-top: 20px;
        }
      }
    }
  }

  .editNameMian {

    .editName {
      display: flex;
      flex-direction: column;
      align-items: center;

      .title {
        text-align: center;
        color: #000D3F;
        font-size: 15px;
        font-weight: 600;
        margin: 10px 0;
      }

      .labelAll {

        margin: 15px 0;

        :deep(.el-checkbox-button__inner) {
          border-radius: 25px;
          border: 1px solid #ccc;
        }

        .labelAllItem {
          margin-left: 20px;
          //margin: 40px 0;
        }
      }

      .btns {
        width: 100%;

        .btn {
          width: 50%;
          height: 60px;
          margin: 0;
          margin-top: 20px;
        }
      }
    }
  }

  .icon-edit {
    display: flex;
    color: #666;
    width: 16px;
    height: 16px;
    background: url('@/icons/编辑.svg') no-repeat center;
    background-size: 16px;
    margin-right: 2px;
  }

  :deep(.el-form-item__content) {

    // justify-content: center;
    // padding: 0 40px;
  }
}
</style>
